package com.zhao.config;

import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
import com.zhao.bean.Employee;
import org.apache.ibatis.reflection.MetaObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;

import javax.servlet.http.HttpSession;
import java.time.LocalDateTime;

@Configuration
public class MybatisPlusMetaObjectHandler implements MetaObjectHandler {

    @Autowired
    private HttpSession session;
    //给那些已经标注了添加填充值的字段赋值
    @Override
    public void insertFill(MetaObject metaObject) {
        Employee employee = (Employee)session.getAttribute("employee");
        metaObject.setValue("createTime", LocalDateTime.now());
        metaObject.setValue("updateTime", LocalDateTime.now());

        metaObject.setValue("createUser", BaseContext.get());
        metaObject.setValue("updateUser", BaseContext.get());
    }
    //给那些已经标注了更新操作，填充值的字段赋值
    @Override
    public void updateFill(MetaObject metaObject) {
        Employee employee = (Employee)session.getAttribute("employee");
        metaObject.setValue("updateTime", LocalDateTime.now());
        metaObject.setValue("updateUser", BaseContext.get());
    }
}
